package com.jxm.channel;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2024-2-7 15:34
 * @Version: 1.0
 */
public class SocketChannelDemo {

    public static void main(String[] args) throws Exception{
//        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("www.baidu.com", 80));
//        ByteBuffer byteBuffer = ByteBuffer.allocate(16);
//        socketChannel.read(byteBuffer);
//        socketChannel.close();
//        System.out.println("read over");

        //以上为阻塞式读，当执行到 read 出，线程将阻塞，控制台将无法打印 read over
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("www.baidu.com", 80));
        socketChannel.configureBlocking(false);
        ByteBuffer byteBuffer = ByteBuffer.allocate(16);
        socketChannel.read(byteBuffer);
        socketChannel.close();
        System.out.println("read over");
    }
}
